package ru.lenwp.util;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import javax.sql.RowSet;
import java.sql.*;

/**
 * Created by Lenar on 30.09.2014.
 */
public class DbUtil {
    public static Long nextId(JdbcTemplate template, String seqName) {
            SqlRowSet rs = template.queryForRowSet("SELECT " + seqName + ".nextval FROM dual");
            rs.next();
            return rs.getLong(1);
    }

    public static int getColumnIndex(ResultSet rs, String column){
        try {
            int count = rs.getMetaData().getColumnCount();
            for(int i=1; i<=count; i++){
                String columnName = rs.getMetaData().getColumnName(i);
                if(columnName.equals(column)){
                    return i;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }
}